﻿#define ROOTSIGNATURE "\
DescriptorTable(CBV(offset = 0, b0, space = 0, numdescriptors = 1)),\
DescriptorTable(CBV(offset = 0, b1, space = 0, numdescriptors = 1)),\
DescriptorTable(SRV(offset = 0, t0, space = 0, numdescriptors = 4)),\
DescriptorTable(Sampler(offset = 0, s0, space = 0, numdescriptors = 1))"

struct IA_TO_VS
{
	float3 Position : Position0;

	//Ivo Zoltan Frey. "Spherical Skinning withDual-Quaternions and Qtangents". SIGGRAPH 2011.
	//snorm float4 QTangent : QTangent0; //QTangent插值 QTangetn GBuffer
	snorm float3 Normal : Normal0;
	snorm float3 Tangent : Tangent0;
	snorm float3 BiTangent : BiTangent0;
	unorm float2 TexCoord_Normal : TexCoord0;
	unorm float2 TexCoord_Albedo : TexCoord1;
	unorm float2 TexCoord_ColorSpecular : TexCoord2;
	unorm float2 TexCoord_Glossiness : TexCoord3;
};

struct VS_TO_RS
{
	float4 Position : SV_Position0;

	//Ivo Zoltan Frey. "Spherical Skinning withDual-Quaternions and Qtangents". SIGGRAPH 2011.
	//snorm float4 QTangent : QTangent0; //QTangent插值 QTangetn GBuffer
	snorm float3 Normal : Normal0;
	snorm float3 Tangent : Tangent0;
	snorm float3 BiTangent : BiTangent0;
	unorm float2 TexCoord_Normal : TexCoord0;
	unorm float2 TexCoord_Albedo : TexCoord1;
	unorm float2 TexCoord_ColorSpecular : TexCoord2;
	unorm float2 TexCoord_Glossiness : TexCoord3;
};

cbuffer g_CBuffer_Sensor:register(b0, space0)
{
	column_major float4x4 g_Sensor_View;
	column_major float4x4 g_Sensor_InverseView;
	column_major float4x4 g_Sensor_Projection;
	column_major float4x4 g_Sensor_InverseProjection;
};

cbuffer g_CBuffer_Mesh:register(b1, space0)
{
	column_major float4x4 g_Mesh_World;
	column_major float4x4 g_Mesh_InverseWorld;
};

[RootSignature(ROOTSIGNATURE)]
VS_TO_RS VertexMain(IA_TO_VS VertexIn)
{
	VS_TO_RS VertexOut;

	VertexOut.Position = float4(VertexIn.Position, 1.0f);
	VertexOut.Position = mul(VertexOut.Position, g_Mesh_World);//WorldSpace
	VertexOut.Position = mul(VertexOut.Position, g_Sensor_View);//WiewSpace
	VertexOut.Position = mul(VertexOut.Position, g_Sensor_Projection);//ProjectionSpace

	float4 Tangent4x1 = float4(VertexIn.Tangent, 1.0f);
	Tangent4x1 = mul(Tangent4x1, g_Mesh_World);//WorldSpace
	//Tangent4x1.xyzw /= Tangent4x1.wwww;
	VertexOut.Tangent = normalize(Tangent4x1.xyz);

	float4 BiTangent4x1 = float4(VertexIn.BiTangent, 1.0f);
	BiTangent4x1 = mul(BiTangent4x1, g_Mesh_World);//WorldSpace
	//BiTangent4x1.xyzw /= BiTangent4x1.wwww;
	VertexOut.BiTangent = normalize(BiTangent4x1.xyz);
	

	float4 Normal1x4 = float4(VertexIn.Normal, 1.0f);
	Normal1x4 = mul(g_Mesh_InverseWorld, Normal1x4);//WorldSpace
	//Normal1x4.xyzw /= Normal1x4.wwww;
	VertexOut.Normal = normalize(Normal1x4.xyz);

	VertexOut.TexCoord_Normal = VertexIn.TexCoord_Normal;
	VertexOut.TexCoord_Albedo = VertexIn.TexCoord_Albedo;
	VertexOut.TexCoord_ColorSpecular = VertexIn.TexCoord_ColorSpecular;
	VertexOut.TexCoord_Glossiness = VertexIn.TexCoord_Glossiness;

	return VertexOut;
}


struct RS_TO_PS
{
	float4 Position : SV_Position0;
	snorm float3 Normal : Normal0;
	snorm float3 Tangent : Tangent0;
	snorm float3 BiTangent : BiTangent0;
	unorm float2 TexCoord_Normal : TexCoord0;
	unorm float2 TexCoord_Albedo : TexCoord1;
	unorm float2 TexCoord_ColorSpecular : TexCoord2;
	unorm float2 TexCoord_Glossiness : TexCoord3;
};

struct PS_TO_OM
{
	snorm float3 GBuffer_Normal : SV_TARGET0;//snorm float
	unorm float3 GBuffer_Albedo : SV_TARGET1;
	unorm float3 GBuffer_ColorSpecular : SV_TARGET2;
	unorm float GBuffer_Glossiness : SV_TARGET3;
};

Texture2D<snorm float3> g_Texture_Normal:register(t0, space0);
Texture2D<unorm float3> g_Texture_Albedo:register(t1, space0);
Texture2D<unorm float3> g_Texture_ColorSpecular:register(t2, space0);
Texture2D<unorm float> g_Texture_Glossiness:register(t3, space0);

SamplerState g_Sampler:register(s0, space0);

[RootSignature(ROOTSIGNATURE)]
[earlydepthstencil]
PS_TO_OM PixelMain(RS_TO_PS PixelIn)
{
	PS_TO_OM PixelOut;
	PixelOut.GBuffer_Normal = g_Texture_Normal.Sample(g_Sampler, PixelIn.TexCoord_Normal);//Normal In TangentSpace
	PixelOut.GBuffer_Normal = mul(PixelOut.GBuffer_Normal, float3x3(PixelIn.Tangent, PixelIn.BiTangent, PixelIn.Normal));//Normal In MeshSpace
	PixelOut.GBuffer_Normal = normalize(PixelOut.GBuffer_Normal);
	PixelOut.GBuffer_Albedo = g_Texture_Albedo.Sample(g_Sampler, PixelIn.TexCoord_Albedo);
	PixelOut.GBuffer_ColorSpecular = g_Texture_ColorSpecular.Sample(g_Sampler, PixelIn.TexCoord_ColorSpecular);
	PixelOut.GBuffer_Glossiness = g_Texture_Glossiness.Sample(g_Sampler, PixelIn.TexCoord_Glossiness);
	return PixelOut;
}